package com.zhxy.manager.controller;


import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.entity.College;
import com.zhxy.manager.service.intf.CollegeService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/college/")
public class CollegeController {
   @Autowired
    private CollegeService service;

    @PostMapping("save.do")
    public R save(College college){

        return service.save(college);
    }
    @GetMapping("page.do")
    public PageBean page(int page, int limit){

        return service.queryPage(page,limit);
    }
    //修改
    @PostMapping("update.do")
    public R update(College college){

        return service.update(college);
    }
    //删除
    @PostMapping("del.do")
    public R del(int id){
        return service.del(id);
    }
    //查询 条件
    @GetMapping("pagewhere.do")
    public PageBean page(String name,  int page, int limit){

        return service.queryPageWhere(name,page,limit);
    }
    //批量删除 JSON格式的数据请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids){
        return service.delBatch(ids);
    }

    //数据导出
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<College> list=service.queryAll();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(),College.class).
                sheet("自动导出").doWrite(list);
    }
    //
    //数据导入，Excel上传-解析-批量新增
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.saveBatch(file);
    }
}
